﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace FotoAnotSharp
{
    /// <summary>
    /// This class represent object in hierarchy. It contains
    /// name, parent, alliases, its objectHierarchy and ancestors,
    /// 
    /// </summary>
    public class GraphicalObject
    {
        public string Name { get; private set; }

        public string FragmentName { get { return "#" + Name; } }

        private HashSet<string> mAlliases = new HashSet<string>();
        public void AddAllias(string al)
        { mAlliases.Add(al); }
        public IEnumerable<string> Alliases
        { get { return mAlliases; } }

        public GraphicalObjectHiearchy Hierarchy { get; private set; }

        public GraphicalObject(GraphicalObjectHiearchy hieararchy, string name)
        {
            Name = name;
            Hierarchy = hieararchy;
        }

        private HashSet<GraphicalObject> mAncestors = new HashSet<GraphicalObject>();
        public void AddAncestor(GraphicalObject obj)
        {
            mAncestors.Add(obj);
            obj.Parrent = this;
        }

        public GraphicalObject Parrent { get; private set; }

        /// <summary>
        /// Iterator through ancestors
        /// </summary>
        public IEnumerable<GraphicalObject> Ancestors           
        { get { return mAncestors; } }

        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return obj is GraphicalObject && Name.Equals((obj as GraphicalObject).Name);
        }

        public override string ToString()
        {
            return Name;
        }

        private Dictionary<CultureInfo, string> mComments = new Dictionary<CultureInfo, string>();
        public void AddComment(CultureInfo language, string comment)
        {
            mComments[language] = comment;
        }

        public string CommentOrName
        {
            get
            {
                string comment;
                if (GetComment(out comment))
                    return comment;
                else
                    return Name;
            }
        }

        public bool GetComment(out string comment)
        { return GetComment(CultureInfo.CurrentUICulture, out comment); }

        public bool GetComment(CultureInfo info, out string comment)
        {
            comment = null;
            if (mComments.TryGetValue(info, out comment))
                return true;
            if (mComments.TryGetValue(CultureInfo.GetCultureInfo(info.TwoLetterISOLanguageName), out comment))
                return true;
            if (mComments.TryGetValue(CultureInfo.GetCultureInfo("en"), out comment))
                return true;
            return false;
        }
    }
}
